package com.gdk.ksoft.assembly.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.gdk.ksoft.assembly.utils.extensions.CanvasUtils;

/**
 * Created by hel on 2014-12-17.
 * 字母索引视图
 */
public class LetterIndexView extends View {

    public interface OnLetterClickListener{
        public void onLetterClick(int selectedIndex,String letter);
    }

    private OnLetterClickListener listener;
    public static final String LETTERS = "ABCDEFGHIJKLMNOPWRSTUVWXYZ#";
    private int viewWidth = 0,viewHeight = 0;//视图宽高
    private int selectedIndex = -1;//被选中的索引
    private Paint paint = new Paint();
    private TextView txtIndictor;//文字指示器

    public TextView getTxtIndictor() {
        return txtIndictor;
    }

    public void setTxtIndictor(TextView txtIndictor) {
        this.txtIndictor = txtIndictor;
    }

    public void setSelectedIndex(int index){
        if(index<0 || index>= LETTERS.toCharArray().length) return;
        selectedIndex = index;
        this.invalidate();
    }

    public int getSelectedIndex(){
        return selectedIndex;
    }

    public void setOnLetterIndexChangedListener(OnLetterClickListener listener){
        this.listener = listener;
    }

    public OnLetterClickListener getOnLetterIndexChangedListener(){
        return listener;
    }

    public LetterIndexView(Context context) {
        super(context);
    }

    public LetterIndexView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public LetterIndexView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 初始化视图
     * @param context
     * @param attr
     * @param defStyleAttr
     */
    private void initView(Context context,AttributeSet attr,int defStyleAttr){

    }

    @Override
    protected void onDraw(Canvas canvas) {//0.8:33
        super.onDraw(canvas);
        paint.reset();
        paint.setAntiAlias(true);
        paint.setTextSize(25f);
        paint.setFakeBoldText(true);
        viewWidth = getMeasuredWidth();
        viewHeight = getMeasuredHeight();
        float fontHeight = CanvasUtils.getFontHeight(paint);
        float pieceHeight = (float)viewHeight/LETTERS.length();
        for(int i=0;i<LETTERS.toCharArray().length;i++){
            paint.setColor(selectedIndex == i? Color.RED:Color.WHITE);
            String textToDraw = String.valueOf(LETTERS.charAt(i));
            float textHeight = paint.measureText(textToDraw);
            canvas.drawText(textToDraw,(viewWidth-textHeight)/2,pieceHeight*i+fontHeight,paint);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int selectedIndex = (int)(event.getY()/((float)viewHeight/LETTERS.toCharArray().length));
        if(selectedIndex<0 || selectedIndex>LETTERS.toCharArray().length) return false;
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                String letter = String.valueOf(LETTERS.charAt(selectedIndex));
                if(txtIndictor!=null){
                    txtIndictor.setVisibility(View.VISIBLE);
                    txtIndictor.setText(letter);
                }
                if(selectedIndex!=this.selectedIndex && listener!=null) {
                    this.selectedIndex = selectedIndex;
                    this.invalidate();
                    listener.onLetterClick(selectedIndex, letter);
                }
                return true;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if(txtIndictor!=null){
                    txtIndictor.setVisibility(View.GONE);
                }
                return true;
        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        viewWidth = MeasureSpec.makeMeasureSpec(50,MeasureSpec.AT_MOST);
        viewHeight = MeasureSpec.makeMeasureSpec(heightMeasureSpec,MeasureSpec.AT_MOST);
        super.onMeasure(viewWidth,viewHeight);
        super.setMeasuredDimension(viewWidth,viewHeight);
    }

}
